package com.cat.greedyAlgorithm;

import java.util.Arrays;

/**
*   @description https://leetcode.cn/problems/find-polygon-with-the-largest-perimeter/
*   @author 曲大人的喵
*   @create 2025/8/22 15:20
*   @since JDK17
*/

public class Solution12 {
    public long largestPerimeter(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        long ans = -1, sum = nums[0] + nums[1];
        for (int i = 2; i < n; i++) {
            if (sum > nums[i]) {
                ans = Math.max(ans, sum + nums[i]);
            }
            sum += nums[i];
        }
        return ans;
    }
}
